package com.palfund.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import java.util.Random;

public class BindService extends Service {
    public BindService() {
        Log.i("BindService", "---BindService()--->" + Thread.currentThread().getName());
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("BindService", "---onCreate()--->" + Thread.currentThread().getName());
    }

    // Activity和Service建立了连接.
    // IBinder:不同组件之间信息传递的通道.
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("BindService", "---onBind()--->" + Thread.currentThread().getName());
        return new CustomBinder();
    }

    // 解除绑定后调用的方法.
    @Override
    public boolean onUnbind(Intent intent) {
        Log.i("BindService", "---onUnbind()--->" + Thread.currentThread().getName());
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("BindService", "---onDestroy()--->" + Thread.currentThread().getName());
    }


    public int getRandom() {
        Random random = new Random();
        return random.nextInt(10);
    }

    class CustomBinder extends Binder {
        public BindService getService() {
            return BindService.this;
        }
    }
}
